package model;

import java.util.List;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;
import com.et.ar.exception.ActiveRecordException;
import com.wzzz.util.StringUtil;
import com.wzzz.util.SysDate;

@Table(name = "t_goods_tcode_manage")
public class Goods_tcode_namage extends ActiveRecordBase {
	@Id
	private String manage_id;
	@Column
	private String company_id;
	@Column
	private String company_code;
	@Column
	private String status;
	@Column
	private String tcode_start;
	@Column
	private String tcode_end;
	@Column
	private String create_by;
	@Column
	private String create_date;
	@Column
	private String update_by;
	@Column
	private String update_date;

	public String getManage_id() {
		return manage_id;
	}

	public void setManage_id(String manage_id) {
		this.manage_id = manage_id;
	}

	public String getCompany_id() {
		return company_id;
	}

	public void setCompany_id(String company_id) {
		this.company_id = company_id;
	}

	public String getCompany_code() {
		return company_code;
	}

	public void setCompany_code(String company_code) {
		this.company_code = company_code;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getTcode_start() {
		return tcode_start;
	}

	public void setTcode_start(String tcode_start) {
		this.tcode_start = tcode_start;
	}

	public String getTcode_end() {
		return tcode_end;
	}

	public void setTcode_end(String tcode_end) {
		this.tcode_end = tcode_end;
	}

	public String getCreate_by() {
		return create_by;
	}

	public void setCreate_by(String create_by) {
		this.create_by = create_by;
	}

	public String getCreate_date() {
		return create_date;
	}

	public void setCreate_date(String create_date) {
		this.create_date = create_date;
	}

	public String getUpdate_by() {
		return update_by;
	}

	public void setUpdate_by(String update_by) {
		this.update_by = update_by;
	}

	public String getUpdate_date() {
		return update_date;
	}

	public void setUpdate_date(String update_date) {
		this.update_date = update_date;
	}
	
	@Override
	public void beforeSave() throws ActiveRecordException {
		//插入前先获取当前要插入单位的最大条形码值，本次的开始位置为最大条形码+1，结束为页面输入的数量
		String companyCode = this.company_code;
		
		//查看当前单位下是否有预打印的条形码
		StringBuffer sqlone= new StringBuffer();
		sqlone.append("SELECT count (1) TCODE_END \n" );
		sqlone.append("  FROM T_GOODS_TCODE_MANAGE T\n" );
		sqlone.append(" WHERE T.STATUS = 250001");
		sqlone.append("   AND T.COMPANY_CODE = '"+companyCode+"'");
		List<Goods_tcode_namage> tmpListone = this.findBySql(Goods_tcode_namage.class, sqlone.toString());
		Goods_tcode_namage tmpone = tmpListone.get(0);
		String tmpEndone = tmpone.getTcode_end();
		if(!"0".equals(tmpEndone)){//说明存在
			throw new ActiveRecordException("");
		}
		
		StringBuffer sql= new StringBuffer();
		sql.append("SELECT NVL(MAX(T.TCODE_END), 0) TCODE_END\n" );
		sql.append("  FROM T_GOODS_TCODE_MANAGE T\n" );
		sql.append(" WHERE T.STATUS <> 250001");
		sql.append("   AND T.COMPANY_CODE = '"+companyCode+"'");

		List<Goods_tcode_namage> tmpList = this.findBySql(Goods_tcode_namage.class, sql.toString());
		Goods_tcode_namage tmp = tmpList.get(0);
		String tmpEnd = tmp.getTcode_end();
		this.tcode_start = Integer.parseInt(tmpEnd)+1+"";
		
		this.tcode_end =  Integer.parseInt(tmpEnd) + Integer.parseInt(this.tcode_end) +"";
		this.tcode_start = StringUtil.fillLeft(this.tcode_start, '0', 6);
		this.tcode_end = StringUtil.fillLeft(this.tcode_end, '0', 6);
		this.status="250001";
	}
	@Override
	public void afterUpdate() throws ActiveRecordException {
		// 如果已经确认下发到下属单位，那么就要生成条形码记录到数据库表中
		if("250003".equals(status)){
			status = "250004";
			//如果已经确认下发到下属单位，那么就要生成条形码记录到数据库表中
			int startPos = Integer.parseInt(this.tcode_start);
			int endPos = Integer.parseInt(this.tcode_end);
			String companyCode = this.company_code;
			String preFix = companyCode.substring(0, 6);
			for(int i=startPos;i<=endPos;i++){
				GoodsTcodeInfo cti = new GoodsTcodeInfo();
				cti.setCompany_code(companyCode);
				String tcodeNum = preFix+StringUtil.fillLeft(Integer.toString(i), '0', 6);
				cti.setGoods_tcode_num(tcodeNum);
				cti.setGoods_tcode_status("0");
				cti.setGoods_tcode_create_date(SysDate.getToday());
				cti.save();
			}
		}
	}
}
